<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="主刀医生" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入主刀医生"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手术时间" prop="ooDate">
        <el-date-picker
          clearable
          v-model="queryParams.ooDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择手术订单时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="挂号id" prop="registerId">
        <el-input
          v-model="queryParams.registerId"
          placeholder="请输入挂号id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住院id" prop="enterId">
        <el-input
          v-model="queryParams.enterId"
          placeholder="请输入住院id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reghos-serv:operation_order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reghos-serv:operation_order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reghos-serv:operation_order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reghos-serv:operation_order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="operation_orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="手术项目id" align="center" prop="operationId"/>
      <el-table-column label="主刀医生" align="center" prop="userName"/>
      <el-table-column label="手术价格" align="center" prop="operationPrice"/>
      <el-table-column label="手术订单时间" align="center" prop="ooDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ooDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手术时间" align="center" prop="operationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="家属是否已签字：是/否" align="center" prop="opertationConfirm"/>
      <el-table-column label="挂号id" align="center" prop="registerId"/>
      <el-table-column label="住院id" align="center" prop="enterId"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['reghos-serv:operation_order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['reghos-serv:operation_order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手术订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手术id" prop="operationId">
          <el-input v-model="form.operationId" placeholder="请输入手术项目id"/>
        </el-form-item>
        <el-form-item label="医生姓名" prop="userName">
          <!-- <el-input v-model="form.userName" placeholder="请选择主刀医生姓名" /> -->
          <el-select v-model="form.userId" placeholder="请选择主刀医生姓名" style="width: 100%;">
            <el-option
              v-for="userName in userNameList"
              :key="userName.userId"
              :label="userName.nickName"
              :value="userName.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="手术价格" prop="operationPrice">
          <el-input v-model="form.operationPrice" placeholder="请输入手术价格"/>
        </el-form-item>
        <el-form-item label="订单时间" prop="ooDate">
          <el-date-picker
            clearable
            v-model="form.ooDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择手术订单时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="手术时间" prop="operationDate">
          <el-date-picker
            clearable
            v-model="form.operationDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择手术时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="是否签字" prop="opertationConfirm">
          <el-input v-model="form.opertationConfirm" placeholder="请输入家属是否已签字：是/否"/>
        </el-form-item>
        <el-form-item label="挂号id" prop="registerId">
          <el-input v-model="form.registerId" placeholder="请输入挂号id"/>
        </el-form-item>
        <el-form-item label="住院id" prop="enterId">
          <el-input v-model="form.enterId" placeholder="请输入住院id"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserNameList,
  listOperation_order,
  getOperation_order,
  delOperation_order,
  addOperation_order,
  updateOperation_order
} from "@/api/reghos-serv/operation_order";
import { getUser } from "@/api/system/user";
export default {
  name: "Operation_order",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 手术订单表格数据
      operation_orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        ooDate: undefined,
        registerId: undefined,
        enterId: undefined
      },
      userNameList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询手术订单列表 */
    getList() {
      this.loading = true;
      listOperation_order(this.queryParams).then(response => {
        this.operation_orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ooId: undefined,
        operationId: undefined,
        userId: undefined,
        userName: undefined,
        operationPrice: undefined,
        ooDate: undefined,
        operationDate: undefined,
        opertationConfirm: undefined,
        registerId: undefined,
        enterId: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ooId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getUserNameList().then(response => {
        this.userNameList = response.data;
      });
      this.open = true;
      this.title = "添加手术订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      getUserNameList().then(response => {
        this.userNameList = response.data;
      });
      const ooId = row.ooId || this.ids;
      getOperation_order(ooId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改手术订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.ooId != null) {
            getUser(this.form.userId).then(response => {
              this.form.userName = response.data.user.nickName;
              updateOperation_order(this.form)
                .then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                })
                .finally(() => {
                  this.buttonLoading = false;
                });
            });
          } else {
            addOperation_order(this.form)
              .then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ooIds = row.ooId || this.ids;
      this.$modal
        .confirm('是否确认删除手术订单编号为"' + ooIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delOperation_order(ooIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "reghos-serv/operation_order/export",
        {
          ...this.queryParams
        },
        `operation_order_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
